﻿//(function() {
//    if (window.J)
//        return;
var global = false;
var j = window.j = {
    version: "0.0.1",
    C: {},
    Browser: (function() {
        var ua = navigator.userAgent;
        var isOpera = window.opera && ua.match(/opera/gi) ? true : false;
        var isIE = !isOpera && document.all && ua.match(/msie/gi) ? true : false;
        return {
            supportW3C: (document.getElementById) ? true : false,
            isOpera: isOpera,
            isIE: isIE,
            isIE6: isIE && ([/MSIE (\d)\.0/i.exec(ua)][0][1] == 6),
            isNS: (navigator.appName == "Netscape") ? true : false,
            isSafari: !isIE && ua.match(/safari/gi) ? true : false,
            isGecko: !isIE && ua.match(/gecko/gi) ? true : false,
            isFirefox: !isIE && ua.match(/firefox/gi) ? true : false,
            isFF3: !isIE && ua.match(/Firefox\/3/gi) ? true : false
        };
    })(),
    extend: function(destination, source) {
        for (var property in source)
            destination[property] = source[property];
    },
    math: {
        scaleRectZoomOut: function(width, height, scale) {
            if (!isNaN(scale) && scale > 0) {
                var iwidth = width, iheight = width * scale;
                var xs = 1, ys = iheight / height;
                var ts;
                if (xs >= 1 && ys >= 1) {
                    ts = Math.max(xs, ys);
                } else {
                    ts = Math.max(xs, ys);
                }
                width = iwidth / ts;
                height = iheight / ts;
            }
            //返回尺寸对象
            return { Width: width, Height: height }
        }
    }
    ,
    //_J: j.prototype,
    box: function(selector) {
        if (selector) {
            return $(selector);
        }
        else
            return this;
    },
    dom: function(selector) {
        if (selector) {
            return $(selector);
        }
        else
            return this;
    },
    once: function() {
        var returnValue;

        for (var i = 0, length = arguments.length; i < length; i++) {
            var lambda = arguments[i];
            try {
                returnValue = lambda();
                break;
            }
            catch (e) {
            }
        }

        return returnValue;
    },
    $: function(element) {
        if (arguments.length > 1) {
            for (var i = 0, elements = [], length = arguments.length; i < length; i++)
                elements.push($(arguments[i]));
            return elements;
        }
        if ("string" == typeof element)
            element = document.getElementById(element);
        else if (element != null && (element.parentNode == null || element.parentNode.nodeName == "#document-fragment"))
            element = document.getElementById(element.id);

        return element;
    },
    Class: function() {
        //Class: {
        //   create: function() {
        //       return function() { this.initialize.apply(this, arguments); }
        //   }
        //},
        var _class = function() {
            j.extend(this, _class)
            return this.initialize.apply(this, arguments);
        };
        for (i = 0; i < arguments.length; i++) {
            superClass = arguments[i];
            j.extend(_class.prototype, superClass.prototype);
        }
        j.extend(_class, {
            initialize: function(options) {
                this.SetOption(options);
            },
            extend: function(f) {
                j.extend(this, f);
                return this;
            },
            option: {},
            SetOption: function(options) {
                this.option = {};
                j.extend(this.option, _class.option);
                j.extend(this.option, options || {});
            }
        });
        return _class;
    },
    getBrowserWindowSize: function() {
        var de = document.documentElement;
        return {
            'width': (window.innerWidth || (de && de.clientWidth) || document.body.clientWidth),
            'height': (window.innerHeight || (de && de.clientHeight) || document.body.clientHeight)
        }
    },
    bind: function(object, fun) {
        return function() {
            return fun.apply(object, arguments);
        }
    },
    BindAsEventListener: function(object, fun) {
        var args = Array.prototype.slice.call(arguments).slice(2);
        return function(event) {
            return fun.apply(object, [event || window.event].concat(args));
        }
    },
    CurrentStyle: function(element) {
        return element.currentStyle || document.defaultView.getComputedStyle(element, null);
    },
    addEventHandler: function(oTarget, sEventType, fnHandler) {
        if (oTarget.addEventListener) {
            oTarget.addEventListener(sEventType, fnHandler, false);
        } else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    },
    removeEventHandler: function(oTarget, sEventType, fnHandler) {
        if (oTarget.removeEventListener) {
            oTarget.removeEventListener(sEventType, fnHandler, false);
        } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, fnHandler);
        } else {
            oTarget["on" + sEventType] = null;
        }
    },
    each: function(list, fun) {
        for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
    },

    Contains: function(a, b) {
        return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
    },
    setcookie: function(name, value) {
        var expdate = new Date();
        var argv = arguments;
        var argc = arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
        document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : (";  expires=" + expdate.toGMTString()))
        + ((path == null) ? "" : (";  path=" + path)) + ((domain == null) ? "" : (";  domain=" + domain))
        + ((secure == true) ? ";  secure" : "");
    },
    getcookie: function(name) {
        var a = document.cookie.split("; ");
        name += "=";
        for (var i = 0; i < a.length; i++)
            if (a[i].indexOf(name) == 0)
            return unescape(a[i].substr(name.length));
        return "";

    },
    delcookie: function(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = j.getcookie(name);
        var argv = arguments;
        var argc = arguments.length;
        var path = (argc > 1) ? argv[1] : null;
        var domain = (argc > 2) ? argv[2] : null;
        var secure = (argc > 3) ? argv[3] : false;

        document.cookie = name + "=" + escape(cval) + "; Expires=" + exp.toGMTString()
        + ((path == null) ? "" : (";  path=" + path)) + ((domain == null) ? "" : (";  domain=" + domain))
        + ((secure == true) ? ";  secure" : "");
    },
    preload: function(img, callback) {
        if (typeof img == "string")
            img = [img];
        var j = img.length;
        for (var i = 0; i < img.length; i++) {
            var img = document.createElement("img");
            img.onload = function() {
                if (! --j) { if (callback) callback(); }
            }
            img.src = img[i];
        }
    }
};
Function.prototype.bind = function(object) {
    var __method = this;
    return function() {
        __method.apply(object, arguments)
    }
};
Function.prototype.bindAsEventListener = function(object) {
    var __method = this;
    return function(event) {
        __method.call(object, event || window.event)
    }
};


var arrayProto = Array.prototype,
      slice = arrayProto.slice,
      _each = arrayProto.forEach; // use native browser JS 1.6 implementation if available

function each(iterator) {
    for (var i = 0, length = this.length; i < length; i++)
        iterator.bind(this)(this[i], i);
}
if (!_each) _each = each;

j.extend(arrayProto, { each: _each,
    remove: function(obj) { this.each(function(sub, index) { if (obj === sub) { this.splice(this, index); } }); }
});

if (global === true) {
    j.extend(window, j);
}
j.sim = {
    clicka: function(a) {
        if (a && a.nodeName == "A") {
            if (j.Browser.isIE)
                a.click();
            else {
                var evt = document.createEvent("MouseEvents");
                evt.initEvent("click", true, true);
                if (a.dispatchEvent(evt) == true)
                    window.location = a.href;
            }
        }
    }
};

//})();

